
#include <assert.h>
#include <math.h>

#include "machine_state_test.h"

using assembler::MachineState;
using assembler::MachineStateInvalidRegisterIdException;
using assembler::MachineStateStackUnderflowException;

namespace assembler_test {

void MachineStateTest::Test() {
  MachineState state;

  state.set_reg(0, 3.000);
  state.set_reg(2, -1.12);

  assert(fabs(state.reg(0) - 3.000) < 1e-5);
  state.push(state.reg(2));
  state.pop(3);

  assert(fabs(state.reg(3) + 1.12) < 1e-5);

  bool stack_underflow_found = false;
  try {
    state.pop(0);
  } catch (MachineStateStackUnderflowException &e) {
    stack_underflow_found = true;
  }
  assert(stack_underflow_found);

  bool wrong_register_exception_found = false;
  try {
    state.set_reg(MachineState::RegisterCount, 0.00);
  } catch (MachineStateInvalidRegisterIdException &e) {
    wrong_register_exception_found = true;
  }
  assert(wrong_register_exception_found);
}

}  // namespace assembler_test